package com.jxaummd.aquaculture.ui.fragment.first.fun;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

import com.blankj.utilcode.util.ToastUtils;
import com.jxaummd.aquaculture.R;
import com.jxaummd.aquaculture.base.BaseBackFragment;
import com.jxaummd.aquaculture.entity.Device;
import com.jxaummd.aquaculture.util.net.RestClient;
import com.jxaummd.aquaculture.util.net.callback.ISuccess;


public class PhoneFragment extends BaseBackFragment {
    private Device device;
    private EditText mEditText;
    private Switch mSwitch;

    public static PhoneFragment newInstance(Device device) {
        Bundle args = new Bundle();
        PhoneFragment fragment = new PhoneFragment();
        fragment.setArguments(args);
        fragment.device = device;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device_phone, container, false);
        initView(view);
        return view;
    }


    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        mEditText = view.findViewById(R.id.fragment_device_phone_num);
        mSwitch = view.findViewById(R.id.fragment_device_phone_switch);

        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
                mEditText.setEnabled(!isChecked);
                RestClient.builder()
                        .url("http://jxaummd.com:8081/lock/changeMsg.do")
                        .params("isopen",isChecked)
                        .params("id",1)
                        .success(new ISuccess() {
                            @Override
                            public void onSuccess(String response) {
                                if(isChecked){
                                    String phone = mEditText.getText().toString().trim();
                                    RestClient.builder()
                                            .url("http://jxaummd.com:8081/lock//changePhone.do")
                                            .params("phone",phone)
                                            .params("id",1)
                                            .success(new ISuccess() {
                                                @Override
                                                public void onSuccess(String response) {
                                                    ToastUtils.showShort("已开启短信通知");
                                                }
                                            })
                                            .build()
                                            .get();
                                }else {
                                    ToastUtils.showShort("已关闭短信通知");
                                }
                            }
                        })
                        .build()
                        .get();
            }
        });
        toolbar.setTitle("短信设置");
        initToolbarNav(toolbar);
    }

}
